/**
 * 浏览器工具类
 *
 * @author hugan
 * @date 2023/10/4
 */
export default {

  /**
   * @return {boolean} 是否已全屏
   */
  isFullScreen() {
    // console.log(document.fullscreenElement, document.mozFullScreenElement, document.webkitFullscreenElement, document.msFullscreenElement, screen.isFullscreen)
    return !!(document.fullscreenElement
      || document.mozFullScreenElement
      || document.webkitFullscreenElement
      || document.msFullscreenElement
      || screen.isFullscreen)
  },
  toFullScreen() {
    let documentElement = document.documentElement
    let fullMethod = documentElement.requestFullScreen  //W3C
      || documentElement.mozRequestFullScreen  //Firefox
      || documentElement.webkitRequestFullscreen //Chrome, Safari and Opera
      || documentElement.msRequestFullScreen //IE/Edge
    if (fullMethod) fullMethod.call(documentElement)
  },
  exitFullscreen() {
    let exitMethod = document.exitFullscreen  //W3C
      || document.mozCancelFullScreen  //Firefox
      || document.webkitExitFullscreen //Chrome, Safari and Opera
      || document.msExitFullscreen //IE/Edge
    if (exitMethod) exitMethod.call(document)
  },
  /**
   * 切换全屏
   * https://blog.csdn.net/qq_25285531/article/details/130356246
   */
  switchFullScreen() {
    if (!this.isFullScreen()) {
      this.toFullScreen()
    } else {
      this.exitFullscreen()
    }
  },
  /**
   * 全屏.开发调试
   */
  checkFullScreenMethods() {
    //全屏
    const toFull = [
      'requestFullscreen',
      'mozRequestFullScreen',//Firefox
      'webkitRequestFullscreen',//Chrome, Safari , Opera
      'msRequestFullscreen',//IE, Edge
    ]
    let documentElement = document.documentElement
    for (const method of toFull) {
      console.log('toFull', method, documentElement[method])
    }
    //退出全屏
    const exitFull = [
      'exitFullscreen',
      'mozCancelFullScreen',//Firefox
      'webkitExitFullscreen',//Chrome, Safari , Opera
      'msExitFullscreen',//IE, Edge
    ]
    for (const method of exitFull) {
      console.log('exitFull', method, document[method])
    }
  },
  /**
   * (会根据当前状态)旋转元素:
   * .目前只针对'旋转键盘'的业务
   * .目标角度只有: 0,-90,90
   * .当前是[-90,90]度时,始终旋转到0度
   * .没适配[位置布局,margin/padding样式]等情况
   *
   * https://blog.csdn.net/qq_39523111/article/details/107855963
   * @param isClockwise true-顺时针,false-逆时针
   */
  rotate(element, isClockwise) {
    let style = element.style
    // console.log('rotate', isClockwise, element, element.offsetHeight)

    let degrees = 0
    if (style.transform) {
      let str = style.transform
        .replace('rotate(', '')
        .replace('deg)', '')
      degrees = parseInt(str)
    }
    degrees += (isClockwise ? 90 : -90)
    degrees = (360 + degrees) % 360//修正度数范围[0,360)
    // console.log('degree', degrees)

    //会丢失了margin,padding??
    let rect = element.getBoundingClientRect()
    // console.log('rect', rect)
    let width = rect.width
    let height = rect.height
    if (degrees === 0 || degrees === 180) {
      style.top = '0'
      style.left = '0'
      degrees = 0
    } else if (degrees === 90) {
      style.transformOrigin = 'bottom left'
      style.top = `-${height}px`
      style.left = `${document.documentElement.clientWidth - height}px`
    } else if (degrees === 270) {
      style.transformOrigin = 'top left'
      style.top = `${width}px`
      style.left = '0'
    }
    style.position = 'absolute'
    style.transform = `rotate(${degrees}deg)`
  },
}